package com.xhwl.centiotdata.suppliersystem.vehicle.ketop;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import com.xhwl.common.dto.cent.vehicle.http.ketop.query.QueryCommonKeTopDTO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleParkingInfoBO;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;

@Component
public class KeTopSignHandler {

    private <T extends QueryCommonKeTopDTO> T preHandle(T t, String parkId, String serviceCode) {
        t.setParkId(Integer.valueOf(parkId));
        t.setReqId(UUID.randomUUID().toString());
        t.setTs(String.valueOf(System.currentTimeMillis()));
        t.setServiceCode(serviceCode);
        return t;
    }

    public <T extends QueryCommonKeTopDTO> T sign(T t, VehicleParkingInfoBO vehicleParkingInfoBO, String serviceCode) {
        Map<String, Object> map = BeanUtil.beanToMap(preHandle(t, vehicleParkingInfoBO.getParkId(), serviceCode), false, true);

        t.setKey(SecureUtil.signParams(DigestAlgorithm.MD5, map, "&", "=", true, "&", vehicleParkingInfoBO.getAppSecret()).toUpperCase());
        t.setAppId(vehicleParkingInfoBO.getAppId());
        return t;
    }

}
